package com.example.libraryapp.activity.bookDetail;

import com.example.libraryapp.Constant;
import com.example.libraryapp.bean.Book;
import com.example.libraryapp.bean.BookDetail;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.network.base.BaseHttpHandler;
import com.example.libraryapp.network.base.BasePresenter;

/**
 * @author 伍凯铭
 * @since 2020/6/23
 */
public class BookDetailPresenterIml extends BasePresenter<BookDetailContract.View> implements BookDetailContract.Presenter{

    @Override
    public void addFavorite(long userId, long bookId) {
        super.doRequest(getRequestService().addFavorite(userId,bookId),new AddFavoriteHandler());
    }

    @Override
    public void deleteFavorite(long userId, long bookId) {
        super.doRequest(getRequestService().deleteFavorite(userId,bookId),new DelFavoriteHandler());
    }

    @Override
    public void getBookDetail(long bookId, long userId) {
        super.doRequest(getRequestService().getDetail(bookId,userId),new BookHandler());
    }

    @Override
    public void borrowBook(long userId, long bookId) {
        super.doRequest(getRequestService().borrowBook(userId,bookId),new BorrowBookHandler());
    }

    @Override
    public void returnBook(long userId, long bookId) {
        super.doRequest(getRequestService().returnBook(userId,bookId),new ReturnBookHandler());
    }

    class BookHandler extends BaseHttpHandler<BaseResponse<BookDetail>> {

        BookHandler() {
            super(mView);
        }

        @Override
        public void onSuccess(BaseResponse<BookDetail> result) {
            if(mView != null) {
                mView.showContent();
                mView.showBook(result);
            }
        }
    }

    class AddFavoriteHandler extends BaseHttpHandler<BaseResponse> {

        AddFavoriteHandler() {
            super(mView);
        }

        @Override
        public void onSuccess(BaseResponse result) {
            if(mView != null) {
                mView.addFavorite(result);
            }
        }

        @Override
        public void onError(Throwable e) {
            if(mView != null) {
                mView.handleError(e.getMessage());
            }
        }
    }

    class DelFavoriteHandler extends BaseHttpHandler<BaseResponse> {

        DelFavoriteHandler() {
            super(mView);
        }

        @Override
        public void onSuccess(BaseResponse result) {
            if(mView != null) {
                mView.deleteFavorite(result);
            }
        }

        @Override
        public void onError(Throwable e) {
            if(mView != null) {
                mView.handleError(e.getMessage());
            }
        }
    }

    class BorrowBookHandler extends BaseHttpHandler<BaseResponse> {

        BorrowBookHandler() {
            super(mView);
        }

        @Override
        public void onSuccess(BaseResponse result) {
            if(mView != null) {
                mView.handleBorrow(result);
            }
        }

        @Override
        public void onError(Throwable e) {
            if(mView != null) {
                mView.handleError(e.getMessage());
            }
        }
    }

    class ReturnBookHandler extends BaseHttpHandler<BaseResponse> {

        ReturnBookHandler() {
            super(mView);
        }

        @Override
        public void onSuccess(BaseResponse result) {
            if(mView != null) {
                mView.handleReturn(result);
            }
        }

        @Override
        public void onError(Throwable e) {
            if(mView != null) {
                mView.handleError(e.getMessage());
            }
        }
    }
}
